package com.fast.fastenjoy.base.web.tag;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.fast.fastenjoy.base.utils.ApplicationContextUtils;
import com.fast.fastenjoy.base.utils.BeanExUtils;
import com.fast.fastenjoy.base.utils.StringExUtils;
import com.fast.fastenjoy.base.web.tag.tagtemplate.TagTemplate;

/**
 * 
 * @author chenliaohua
 *
 */
public abstract class BaseTag extends TagSupport{

	private static final long serialVersionUID = -64181701702089713L;
	
	protected String id;
	protected String name;
	protected String cssClass;
	protected String cssStyle;
	protected String onblur;
	protected String onchange;
	protected String onclick;
	protected String onfocus;
	protected String onkeydown;
	protected String onkeyup;
	protected String onmousemove;
	protected String onmouseout;
	protected String onmouseover;
	protected String onselect;
	
	protected TagTemplate tagTemplate = (TagTemplate)ApplicationContextUtils.getBean("tagTemplate");
	
	protected JSONObject json = new JSONObject();

	public abstract void doInitParam();
	
	public abstract void doTag();
	
	@Override
	public int doStartTag() throws JspException{
			
		doInitParam();
		
		doTag();
			
		return SKIP_BODY;
	}
	
	protected void writeHtml(String html){
		try {
			this.pageContext.getOut().write(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private String getBaseTagHtml() {
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		putBaseParam("b_id", paramMap)
		.putBaseParam("ctx", paramMap)
		.putBaseParam("b_name", paramMap)
		.putBaseParam("b_cssClass", paramMap)
		.putBaseParam("b_cssStyle", paramMap)
		.putBaseParam("b_onblur", paramMap)
		.putBaseParam("b_onchange", paramMap)
		.putBaseParam("b_onclick", paramMap)
		.putBaseParam("b_onfocus", paramMap)
		.putBaseParam("b_onkeydown", paramMap)
		.putBaseParam("b_onkeyup", paramMap)
		.putBaseParam("b_onmousemove", paramMap)
		.putBaseParam("b_onmouseout", paramMap)
		.putBaseParam("b_onmouseover", paramMap)
		.putBaseParam("b_onselect", paramMap);
		
		return StringExUtils.$format(getTagTemplate(Tags.BASE_TAG), paramMap);
	}
	
	private BaseTag putBaseParam(String key, Map<String, Object> paramMap){
		
		if(StringExUtils.isNotEmpty(key)){
			
			try{
				
				String tempKey = key;
				
				String propertyName = tempKey.substring(2);
				
				Object propertyValue = BeanExUtils.getBeanProperty(this, propertyName);
				
				String propertyValueStr = propertyValue == null ? StringUtils.EMPTY : String.valueOf(propertyValue);
				String domHtml = StringUtils.EMPTY;
				
				if(StringExUtils.isNotBlank(propertyValueStr)){
					domHtml = new StringBuffer(" ").append(propertyName).append("=\"").append(propertyValueStr).append("\"").toString();
				}
				
				paramMap.put(tempKey, domHtml);
				
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		return this;
	}
	
	protected String getBaseTagHtml(String html) {
		
		String baseTagHtml = getBaseTagHtml();
		
		baseTagHtml = baseTagHtml.replaceAll("\\r|\\n", StringUtils.EMPTY);
		
		String tempHtml = StringExUtils.$format(html, "bastTag", baseTagHtml);
		
		tempHtml = StringExUtils.$format(tempHtml, getCommonParam());
		
		return tempHtml;
	}

	protected Map<String, Object> getCommonParam(){
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
		paramMap.put("ctx", request.getContextPath());
		
		return paramMap;
	}
	
	public String getTagTemplate(String tagTemplateId){
		return tagTemplate.getTagTemplate(tagTemplateId);
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getCssStyle() {
		return cssStyle;
	}

	public void setCssStyle(String cssStyle) {
		this.cssStyle = cssStyle;
	}

	public String getOnblur() {
		return onblur;
	}

	public void setOnblur(String onblur) {
		this.onblur = onblur;
	}

	public String getOnchange() {
		return onchange;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public String getOnclick() {
		return onclick;
	}

	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

	public String getOnfocus() {
		return onfocus;
	}

	public void setOnfocus(String onfocus) {
		this.onfocus = onfocus;
	}

	public String getOnkeydown() {
		return onkeydown;
	}

	public void setOnkeydown(String onkeydown) {
		this.onkeydown = onkeydown;
	}

	public String getOnkeyup() {
		return onkeyup;
	}

	public void setOnkeyup(String onkeyup) {
		this.onkeyup = onkeyup;
	}

	public String getOnmousemove() {
		return onmousemove;
	}

	public void setOnmousemove(String onmousemove) {
		this.onmousemove = onmousemove;
	}

	public String getOnmouseout() {
		return onmouseout;
	}

	public void setOnmouseout(String onmouseout) {
		this.onmouseout = onmouseout;
	}

	public String getOnmouseover() {
		return onmouseover;
	}

	public void setOnmouseover(String onmouseover) {
		this.onmouseover = onmouseover;
	}

	public String getOnselect() {
		return onselect;
	}

	public void setOnselect(String onselect) {
		this.onselect = onselect;
	}

	public JSONObject getJson() {
		return json;
	}

	public void setJson(JSONObject json) {
		this.json = json;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
}
